<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小学数学题生成器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        #controls {
            margin-bottom: 20px;
        }
        #questions, #preview {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 15px;
        }
        .question {
            font-size: 20px;
            text-align: center;
        }
        #preview {
            display: none;
            background-color: white;
            border: 1px solid #ccc;
            padding: 20px;
            margin-top: 20px;
        }
        #preview .question {
            font-size: 18px;
        }
        @media print {
            body {
                font-size: 16px;
            }
            #controls, #questions, #previewButton {
                display: none;
            }
            #preview {
                display: grid !important;
                border: none;
                padding: 0;
            }
        }
        .disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <div id="controls">
        <label>题目数量: <input type="number" id="questionCount" value="100" min="1" max="200"></label><br>
        <label>数字范围: <input type="number" id="numberRange" value="20" min="1" max="100"></label><br>
        <label>结果范围: <input type="number" id="resultRange" value="20" min="1" max="100"></label><br>
        <button onclick="generateQuestions()">生成题目</button>
        <button onclick="showPrintPreview()" id="previewButton" class="disabled" disabled>打印预览</button>
        <button onclick="printQuestions()" id="printButton" class="disabled" disabled>打印题目</button>
    </div>
    <div id="questions"></div>
    <div id="preview"></div>

    <script>
        let questionsGenerated = false;

        function generateQuestions() {
            const questionsContainer = document.getElementById('questions');
            questionsContainer.innerHTML = '';

            const count = parseInt(document.getElementById('questionCount').value);
            const numberRange = parseInt(document.getElementById('numberRange').value);
            const resultRange = parseInt(document.getElementById('resultRange').value);

            for (let i = 0; i < count; i++) {
                const question = generateQuestion(numberRange, resultRange);
                const questionElement = document.createElement('div');
                questionElement.className = 'question';
                questionElement.textContent = question;
                questionsContainer.appendChild(questionElement);
            }

            questionsGenerated = true;
            document.getElementById('previewButton').classList.remove('disabled');
            document.getElementById('previewButton').disabled = false;
            document.getElementById('printButton').classList.remove('disabled');
            document.getElementById('printButton').disabled = false;
        }

        function generateQuestion(numberRange, resultRange) {
            const operators = ['+', '-'];
            const operator = operators[Math.floor(Math.random() * operators.length)];
            let a, b, result;

            do {
                a = Math.floor(Math.random() * numberRange) + 1;
                b = Math.floor(Math.random() * numberRange) + 1;
                result = operator === '+' ? a + b : a - b;
            } while (result < 0 || result > resultRange);

            return `${a} ${operator} ${b} = `;
        }

        function showPrintPreview() {
            if (!questionsGenerated) {
                alert('请先生成题目！');
                return;
            }
            updatePreview();
            document.getElementById('preview').style.display = 'grid';
            document.getElementById('questions').style.display = 'none';
            document.getElementById('previewButton').textContent = '返回编辑';
            document.getElementById('previewButton').onclick = hidePrintPreview;
        }

        function hidePrintPreview() {
            document.getElementById('preview').style.display = 'none';
            document.getElementById('questions').style.display = 'grid';
            document.getElementById('previewButton').textContent = '打印预览';
            document.getElementById('previewButton').onclick = showPrintPreview;
        }

        function updatePreview() {
            const previewContainer = document.getElementById('preview');
            const questionsContainer = document.getElementById('questions');
            previewContainer.innerHTML = questionsContainer.innerHTML;
        }

        function printQuestions() {
            if (!questionsGenerated) {
                alert('请先生成题目！');
                return;
            }
            updatePreview();
            document.getElementById('preview').style.display = 'grid';
            document.getElementById('questions').style.display = 'none';
            window.print();
            setTimeout(() => {
                document.getElementById('preview').style.display = 'none';
                document.getElementById('questions').style.display = 'grid';
            }, 100);
        }
    </script>
</body>
</html>